package com.product.utile;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: CheckUtil
 * 字段check
 * @date: 2021/3/23
 * @author: 王瑾
 */
public class CheckUtil {
    /**
     * 批量判断是否为空
     * @param map <项目名称，项目内容>
     * @throws Exception
     */
    public static void isEmpty(Map<String, Object> map) throws Exception {
        for (String key : map.keySet()) {
            if (StringUtil.isEmpty(map.get(key).toString())) {
                throw new Exception(key + "不能为空");
            }

        }
    }

    /**
     * 批量判断是否超长长度
     * @param map <长度，项目<项目名称，项目内容>>
     * @throws Exception
     */
    public static void isToLong(Map<Integer, JSONObject> map) throws Exception {
        for (int key : map.keySet()) {
            for (Object keys : map.get(key).keySet()) {
                if(CheckUtil.isBeyondLength(map.get(key).get(keys).toString(),key)){
                    throw new Exception(keys + "长度超出限制");
                }
            }
        }
    }

    /**
     * 文字长度检查<br>
     * <br>
     * @param pInput     要检查的字符串<br>
     * @param pLength    列数<br>
     * @return boolean   返回检查结果<br>
     *      
     */
    public static boolean isBeyondLength (String pInput,int pLength){
        if (pLength < 0) {
            return true;
        } else if (pInput != null) {
            if (pInput.getBytes().length > pLength) {
                return true;
            }
        }
        return false;
    }

    public static void main (String[]args){
        Map<Integer, JSONObject> maps = new HashMap<>();
        maps.put(10, new JSONObject().fluentPut("张三", "JSON").fluentPut("历史", "JON1111111"));
        maps.put(11, new JSONObject().fluentPut("沾沾自喜",11));
        Map<String, Object> map = new HashMap<>();
        map.put("历史", 1);
        map.put("阿萨德", Arrays.asList("1", "b", "c"));
        map.put("一", "1");
        map.put("二", "aa");
        map.put("实施", "aa");
        try {
            CheckUtil.isEmpty(map);
            CheckUtil.isToLong(maps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}